package com.unionftech.common.webview.impl

import com.unionftech.common.webview.callback.BaseProgressSpec

/**
 * 进度条的控制类
 */
class IndicatorHandler {
    /**
     * 进度条接口
     */
    var baseProgressSpec: BaseProgressSpec? = null

    companion object {
        @Volatile
        private var instance: IndicatorHandler? = null

        @JvmStatic
        fun getInstance() = instance ?: synchronized(this) {
            instance ?: IndicatorHandler().also {
                instance = it
            }
        }
    }

    /**
     * 更新进度
     */
    fun progress(progress: Int) {
        when (progress) {
            0 -> reset()
            in 1..10 -> showProgressBar()
            in 11..94 -> setProgressBar(progress)
            else -> {
                setProgressBar(progress)
                finish()
            }
        }
    }

    private fun reset() {
        baseProgressSpec?.reset()
    }

    private fun showProgressBar() {
        baseProgressSpec?.show()
    }

    private fun setProgressBar(progress: Int) {
        baseProgressSpec?.setProgress(progress)
    }

    private fun finish() {
        baseProgressSpec?.hide()
    }

    fun inJectProgressView(baseProgressSpec: BaseProgressSpec?): IndicatorHandler {
        this.baseProgressSpec = baseProgressSpec
        return this
    }

}